* Figure 7 Year-by-year effects of individual-level insurance coverage (IV regressions)
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if bal_panel3==1

sort id mkt_year

replace Tvillage = 0  if mkt_year == 2005

* marketing instruments
local uniqinst "bdmperc_20* fourbdmperc_20* disc4game_20* assigned_risk_ws_2008 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan discount_20* rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv groupT muslimT hinduT sewaT peerT ppayT vframeT pframeT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013" /*mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}
	
rename fraction_hy HY
rename fraction_cashcrop cashcrop

local outcomevars "totalrevenues_w1_real kval_w1_real fin_revenues_real totalcosts_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 fin_costs_real totalprofit_w1_real profit_k_w1_real  HY cashcrop plotsize_ha"

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

* Easier labelling for graphs
lab var cost_k_w1_real "Total agricultural costs (INR)"
lab var profit_k_w1_real "Total agricultural profits (INR)"
lab var kval_w1_real "Total agricultural revenues (INR)"
lab var fin_costs_real "Total insurance costs (INR)"
lab var fin_revenues_real "Total insurance revenues (INR)"
lab var totalcosts_w1_real "Total costs (INR)"
lab var totalrevenues_w1_real "Total revenues (INR)"
lab var totalprofit_w1_real "Total profit (INR)"
lab var HY "Fraction of HYV crops"
lab var cashcrop "Fraction of cash-crops"
lab var plotsize_ha "Area cultivated (in hectares)"


// Year-by-year effects IV - Ind 
foreach ovar of varlist cost_k_w1_real kval_w1_real profit_k_w1_real fin_costs_real fin_revenues_real totalcosts_w1_real totalrevenues_w1_real totalprofit_w1_real HY cashcrop plotsize_ha {
gen `ovar'effect = .
gen `ovar'se = .
gen `ovar'se2 = .
gen `ovar'se_2 = .
forvalues i = 2006/2013 {
ivreg `ovar' (s_policy_units=treat_year `uniqinst') firstt_2007 lost_w3 resurveyed if mkt_year == `i' , r cluster(villageno)
replace `ovar'effect = _b[s_policy_units] if mkt_year == `i'
replace `ovar'se = _se[s_policy_units] if mkt_year == `i'
lab var `ovar'effect "No. of Policies Purhchased - Effect"
lab var `ovar'se "Standard Errors"
replace `ovar'se2  = `ovar'effect+2*`ovar'se if mkt_year == `i'
replace `ovar'se_2 = `ovar'effect-2*`ovar'se if mkt_year == `i'
}

local l`ovar': variable label `ovar'
twoway (rarea `ovar'se2 `ovar'se_2 mkt_year, sort astyle(ci ci))(scatter `ovar'effect mkt_year , sort msymbol(O) c(l)), title("`l`ovar''", size(s)) ylabel(,angle (30)) xlabel(2005(2)2013) xtitle("Year") graphregion(color(white)) plotregion(icolor(white)) legend(order (2 1) label(2 "Estimated coefficient") label(1 "95% confidence interval"))
graph save iv`ovar', replace 
}

grc1leg ivcost_k_w1_real.gph ivkval_w1_real.gph ivprofit_k_w1_real.gph ivfin_costs_real.gph ivfin_revenues_real.gph ivtotalcosts_w1_real.gph ivtotalrevenues_w1_real.gph ivtotalprofit_w1_real.gph ivHY.gph ivcashcrop.gph ivplotsize_ha.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/f7.emf", replace


